package com.card.hwatt.assembler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;

import com.card.hwatt.core.ObjectUtil;
import com.card.hwatt.entity.*;

/**
 * 时间段
 * @author Administrator
 *
 */
public final class WorkTimeAssembler {

	public static List<com.card.hwatt.dto.WorkTime> convert(List<WorkTime> list) {
		List<com.card.hwatt.dto.WorkTime> rows = new ArrayList<com.card.hwatt.dto.WorkTime>();
		for (com.card.hwatt.entity.WorkTime item : list) {
			com.card.hwatt.dto.WorkTime dto = convert(item);
			rows.add(dto);
		}
		return rows;
	}

	public static com.card.hwatt.dto.WorkTime convert(WorkTime entity) {
		com.card.hwatt.dto.WorkTime dto = new com.card.hwatt.dto.WorkTime();
		ObjectUtil.fieldCopy(entity, dto);

		String pattern = "H:mm";
		Calendar startTime = Calendar.getInstance();
		startTime.set(2000, 1, 1,
				Integer.valueOf(entity.startTime.substring(0, 2)),
				Integer.valueOf(entity.startTime.substring(2, 4)));

		dto.startTime = DateFormatUtils.format(startTime, pattern);

		Calendar startBestTime = Calendar.getInstance();
		startBestTime.setTime(startTime.getTime());
		startBestTime.add(Calendar.SECOND, -entity.beforeStart * 60);
		dto.startBestTime = DateFormatUtils.format(startBestTime, pattern)
				+ "-" + dto.startTime;

		Calendar endTime = Calendar.getInstance();
		endTime.setTime(startTime.getTime());
		endTime.add(Calendar.MINUTE, entity.endTime);
		dto.endTime = DateFormatUtils.format(endTime, pattern);

		Calendar endBestTime = Calendar.getInstance();
		endBestTime.setTime(endTime.getTime());
		endBestTime.add(Calendar.SECOND, entity.afterEnd * 60);
		dto.endBestTime = dto.endTime + "-"
				+ DateFormatUtils.format(endBestTime, pattern);

		DecimalFormat df = new DecimalFormat("#0.##");
		dto.hour = Double.parseDouble(df.format((double) entity.endTime / 60D));

		return dto;
	}

	public static WorkTime convert(com.card.hwatt.dto.WorkTime dto) {
		WorkTime entity = new WorkTime();
		ObjectUtil.fieldCopy(dto, entity);

		Calendar calendar = Calendar.getInstance();
		int start = Integer.valueOf(dto.startTime.split(":")[0]);
		calendar.set(2000, 1, 1, start,
				Integer.valueOf(dto.startTime.split(":")[1]), 0);
		String pattern = "HHmm";
		entity.startTime = DateFormatUtils.format(calendar, pattern);

		Calendar endTime = Calendar.getInstance();
		int end = Integer.valueOf(dto.endTime.split(":")[0]);
		endTime.set(2000, 1, 1 + (start < end ? 0 : 1), end,
				Integer.valueOf(dto.endTime.split(":")[1]), 0);

		entity.endTime = Math.abs(millisecondsToDays(endTime, calendar));

		return entity;
	}

	private static int millisecondsToDays(Calendar endTime, Calendar calendar) {
		double todayMs = Math.round(Double
				.valueOf(calendar.getTime().getTime()) / 1000D);
		double returnMs = Math.round(Double
				.valueOf(endTime.getTime().getTime()) / 1000D);
		double intervalMs = todayMs - returnMs;
		return Double.valueOf(Math.round(intervalMs / 60D)).intValue();
	}
}
